package com.sgai.zero_robot.pop;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialog;


public abstract class BaseDialog extends AppCompatDialog {

    /** 宽度和高度   */
    private int mWidth = WindowManager.LayoutParams.WRAP_CONTENT;
    private int mHeight = WindowManager.LayoutParams.WRAP_CONTENT;

    /** 重心位置   */
    private int mGravity = Gravity.NO_GRAVITY;

    /** 水平偏移   */
    private int mXOffset = 0;

    /** 垂直偏移   */
    private int mYOffset = 0;

    public BaseDialog(@NonNull Context context) {
        this(context, -1);
    }

    public BaseDialog(@NonNull Context context, int theme) {
        super(context, theme);
        initView(context);
    }

    protected BaseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initView(context);
    }

    private void initView(Context context) {
        View mContentView = LayoutInflater.from(context).inflate(getLayoutId(), new FrameLayout(context), false);
        setContentView(mContentView);
        initContentView(mContentView);
        ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams();
        Window window = getWindow();
        mGravity = getGravity();
        mWidth = getDialogWidth();
        mHeight = getDialogHeight();
        mXOffset = getXOffset();
        mYOffset = getYOffset();

        // 如果当前没有设置重心，就自动获取布局重心
        if (mGravity == Gravity.NO_GRAVITY) {
            if (layoutParams instanceof FrameLayout.LayoutParams) {
                FrameLayout.LayoutParams frameLayoutParams = (FrameLayout.LayoutParams) layoutParams;
                int gravity = frameLayoutParams.gravity;
                if (gravity != FrameLayout.LayoutParams.UNSPECIFIED_GRAVITY) {
                    if (window != null) {
                        window.setGravity(gravity);
                    }
                }
            } else if (layoutParams instanceof LinearLayout.LayoutParams) {
                LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) layoutParams;
                int gravity = linearLayoutParams.gravity;
                if (gravity != FrameLayout.LayoutParams.UNSPECIFIED_GRAVITY) {
                    if (window != null) {
                        window.setGravity(gravity);
                    }
                }
            }
            if (mGravity == Gravity.NO_GRAVITY) {
                // 默认重心是居中
                if (window != null) {
                    window.setGravity(Gravity.CENTER);
                }
            }
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            }
        }

        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            if (layoutParams != null && mWidth == ViewGroup.LayoutParams.WRAP_CONTENT && mHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
                // 如果当前 Dialog 的宽高设置了自适应，就以布局中设置的宽高为主
                params.width = layoutParams.width;
                params.height = layoutParams.height;
            } else {
                params.width = mWidth;
                params.height = mHeight;
            }
            params.gravity = mGravity;
            params.x = mXOffset;
            params.y = mYOffset;
            if (getBackgroundDimEnabled()) {
                window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                window.setDimAmount(0.5f);
            } else {
                window.clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
            }
            window.setAttributes(params);
        }

        setCancelable(getCancelable());
        setCanceledOnTouchOutside(getCanceledOnTouchOutside());
    }

    public boolean getCanceledOnTouchOutside() {
        return false;
    }

    public boolean getCancelable() {
        return false;
    }

    public boolean getBackgroundDimEnabled() {
        return true;
    }

    public int getYOffset() {
        return 0;
    }

    public int getXOffset() {
        return 0;
    }

    protected abstract int getDialogHeight();

    protected abstract int getDialogWidth();

    public int getGravity() {
        return Gravity.NO_GRAVITY;
    }

    protected abstract void initContentView(@Nullable View mContentView);

    protected abstract int getLayoutId();
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus) {
            hideSystemUI();
        }
        super.onWindowFocusChanged(hasFocus);
    }
    private void hideSystemUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            WindowInsetsController controller = getWindow().getInsetsController();
            if (controller != null) {
                controller.hide(WindowInsets.Type.navigationBars() | WindowInsets.Type.statusBars());
                controller.setSystemBarsBehavior(
                        WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                );
            }
        } else {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            );
        }
    }
}